package com.CST.service.impl;

import com.CST.common.api.Response;
import com.CST.common.constant.Constants;
import com.CST.common.enumPackge.EnumIsUse;
import com.CST.dto.DateDTO;
import com.CST.entity.RdCostsalaryrelation;
import com.CST.mapper.cst.RdCostsalaryrelationMapper;
import com.CST.service.IRdCostsalaryrelationService;
import com.CST.util.ExcelTransfer;
import com.CST.util.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-12-08
 */
@Service
public class RdCostsalaryrelationServiceImpl extends ServiceImpl<RdCostsalaryrelationMapper, RdCostsalaryrelation> implements IRdCostsalaryrelationService {

    @Override
    public List<RdCostsalaryrelation> getRdCostSalaryRelation(RdCostsalaryrelation entity) {
        return baseMapper.getRdCostSalaryRelation(entity);
    }

    public void deleteByYearMonthAndRule(String... obj) {
        baseMapper.deleteByYearMonthAndRule(obj[0], obj[1]);
    }


    @Override
    public Response insertBatchRdCostSalaryRelation(List<RdCostsalaryrelation> List) {
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            int total = List.size();//为防止插入字符超过最大数,分批插入
            int fromIndex = 0;
            List<RdCostsalaryrelation> insertList = new ArrayList<>();
            for (int i = 0; i < List.size(); i++) {
                RdCostsalaryrelation item = List.get(i);
                item.setRowStatus("0");
                item.setCreatedBy(userName);
                item.setUpdatedBy(userName);
                item.setCreatedOn(sdf.format(currDate));
                item.setUpdatedOn(sdf.format(currDate));
                fromIndex++;
                insertList.add(item);
                if (item.getRemark() == null) {
                    item.setRemark("");
                }
                if (fromIndex % 3 == 0 && fromIndex <= total) {
                    baseMapper.insertBatchRdCostSalaryRelation(insertList);
                    insertList = new ArrayList<>();
                }
            }
            if (CollectionUtils.isNotEmpty(insertList)) {
                baseMapper.insertBatchRdCostSalaryRelation(insertList);
            }
        } catch (Exception ex) {
            int a =0;
            return Response.error(String.join(":", "批量添加失败:", ex.getMessage()));
        }
        return Response.success("批量添加成功");
    }

    @Override
    public Response modifyRdCostSalaryRelation(RdCostsalaryrelation entity) {
        if (entity.getId() == 0) {
            return Response.error("主键不能为空");
        }
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            entity.setCreatedOn(sdf.format(currDate));
            entity.setUpdatedOn(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setCreatedBy(userName);
            entity.setUpdatedBy(userName);
            baseMapper.modifyRdCostSalaryRelation(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "修改失败", ex.getMessage()));
        }
        return Response.success("修改成功");
    }

    @Override
    public Response batchDeleteRdCostSalaryRelation(List<Integer> ids) {
        try {
            baseMapper.batchDeleteRdCostSalaryRelation(ids);
        } catch (Exception ex) {
            return Response.error("删除失败");
        }
        return Response.success("删除成功");
    }

    @Override
    public List<RdCostsalaryrelation> listCostItemByDate(DateDTO dateDTO) {
        List<RdCostsalaryrelation> list = new ArrayList<>();
        list.add(RdCostsalaryrelation.builder().costItemCode("grhj").costItemName("个人合计").build());
        list.addAll(baseMapper.listCostItemByDate(dateDTO));
        return list;
    }

    @Autowired
    private ExcelTransfer<RdCostsalaryrelation> excelTransfer;

    @Override
    public void downloadExcel(HttpServletResponse response, RdCostsalaryrelation entity) {
        List<RdCostsalaryrelation> list = baseMapper.getRdCostSalaryRelation(entity);
        list.forEach(e -> e.setRowStatus(
                "0".equals(e.getRowStatus()) ? EnumIsUse.USE.toString() : EnumIsUse.NO_USE.toString()));
        excelTransfer.exportExcel(response, list, "成本-薪酬系统", "sheet", RdCostsalaryrelation.class);
    }

    @Override
    public List<RdCostsalaryrelation> checkRelation(DateDTO dateDTO) {
        return baseMapper.checkRelation(dateDTO);
    }

}
